package serv.midser.wxaction;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import serv.utils.ApiSignUtils;
import serv.utils.HttpUtils;
import serv.utils.JsonUtils;
import serv.utils.encry.HashUtils;

public class WxActiveAccessApiAdapterService {
	
	/**
	 * 调用接口服务的基础地址
	 */
	private String apiServerUrl;
	
	private String apiAccessId;
	
	private String apiPrivateKey;
	
	/**
	 * 发布的基础地址
	 */
	private String publishUrl;
	
	public static void main(String[] arges) throws IOException {
		WxActiveAccessApiAdapterService service = new WxActiveAccessApiAdapterService();
		service.setApiAccessId("sx_unicom_elect");
		service.setApiPrivateKey("sx_unicom_elect");
		service.setApiServerUrl("http://wo.sn.cn/wxActive/");
		List<Map<String, Object>> list = service.getAccessApiStat(new Date(), null, null);
		for (Map<String, Object> o : list) {
			System.out.println(JsonUtils.toJson(o));
		}
	}
	
	/**
	 * 获取应用管理页面
	 * @param accessId
	 * @param privateKey
	 * @return
	 */
	public String getAppManagerUrl(String accessId, String privateKey, String group) {
		if (group == null) group = "wxActive";
		String url = publishUrl + "app/center/"+group+"/"+accessId+"/index.do";
		return ApiSignUtils.rebuildParmars(url, "sign", privateKey);
	}
	
	/**
	 * 添加或更新应用信息
	 * @param accessId
	 * @param privateKey
	 * @param appName
	 * @param contactPersonName
	 * @param contactPhone
	 * @throws Exception 
	 */
	public void setAppAccess(
			String accessId, String privateKey, 
			String appName, 
			String contactPersonName, String contactPhone
			) throws Exception {
		if (apiServerUrl == null || apiAccessId == null || apiPrivateKey == null) {
			throw new Exception("no config wx action server info.");
		}
		//
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("accessId", accessId);
		data.put("privateKey", privateKey);
		data.put("appName", appName);
		data.put("contactPersonName", contactPersonName);
		data.put("contactPhone", contactPhone);
		//
		String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String sign = HashUtils.md5(apiAccessId + apiPrivateKey + timestamp);
		//
		String url = apiServerUrl + "app/api/setAppAccess.do?accessId="+apiAccessId+"&timestamp="+timestamp+"&sign="+sign;
		Map<String, String> parmars = new HashMap<String, String>();
		parmars.put("json", JsonUtils.toJson(data));
		String res = HttpUtils.post(url, parmars);
		Map<String, Object> resObj = JsonUtils.parseJsonMap(res);
		if (!"success".equals(resObj.get("state"))) {
			throw new RuntimeException("invork api faild. res[" + res + "]");
		}
	}
	
	/**
	 * 查询APP统计信息
	 * @param apiAccessId
	 * @param startTime
	 * @param endTime
	 * @param accessIds
	 * @return
	 * @throws IOException
	 */
	public List<Map<String, Object>> getAccessApiStat(
			Date startTime, Date endTime, List<String> accessIds
	) throws IOException {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("apiAccessId", apiAccessId);
		data.put("startTime", startTime);
		data.put("endTime", endTime);
		data.put("accessIds", accessIds);
		//
		String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String sign = HashUtils.md5(apiAccessId + apiPrivateKey + timestamp);
		//
		String url = apiServerUrl + "app/api/stat/accessApiStat.do?accessId="+apiAccessId+"&timestamp="+timestamp+"&sign="+sign;
		Map<String, String> parmars = new HashMap<String, String>();
		parmars.put("json", JsonUtils.toJson(data));
		String res = HttpUtils.post(url, parmars);
		Map<String, Object> resObj = JsonUtils.parseJsonMap(res);
		if (!"success".equals(resObj.get("state"))) {
			throw new RuntimeException("invork api faild. res[" + res + "]");
		}
		return (List<Map<String, Object>>) resObj.get("data");
	}

	public String getApiServerUrl() {
		return apiServerUrl;
	}

	public void setApiServerUrl(String apiServerUrl) {
		this.apiServerUrl = apiServerUrl;
	}

	public String getApiAccessId() {
		return apiAccessId;
	}

	public void setApiAccessId(String apiAccessId) {
		this.apiAccessId = apiAccessId;
	}

	public String getApiPrivateKey() {
		return apiPrivateKey;
	}

	public void setApiPrivateKey(String apiPrivateKey) {
		this.apiPrivateKey = apiPrivateKey;
	}

	public String getPublishUrl() {
		return publishUrl;
	}

	public void setPublishUrl(String publishUrl) {
		this.publishUrl = publishUrl;
	}
	
	
	

}
